/*
 * UIEngine.cpp
 */

#include "UIEngine.h"
#include "UIComposite.h"
#include <iostream>
#include <typeinfo>

UIEngine::UIEngine(RGBAColor background, RGBAColor border, float width,
    float height, float borderSize, float rounding) :
  _colorBackground(background), _colorBorder(border),
      _widthTopContainer(width), _heightTopContainer(height), _borderSize(
          borderSize), _roundingCorner(rounding), _currentDepth(0.) {
}

UIEngine::~UIEngine() {
  _uiComponents.clear();
}

void UIEngine::addUIComponent(UIComponent* uic) {
  // check if the UI Component is composite
  // if so, set color theme to this container
  UIComposite* comp = dynamic_cast<UIComposite*> (uic);
  if (comp != 0) {
    comp->setColors(_colorBackground, _colorBorder);
    comp->setBorderSize(_borderSize);
    comp->setRoundingCorner(_roundingCorner);
    if (comp->getType() == UIComposite::TOPCONTAINER) {
      comp->setDimensions(_widthTopContainer, _heightTopContainer);
    }
  }
  uic->setDepth(_currentDepth);
  _currentDepth -= 0.01;
  _uiComponents.push_back(uic);
}

void UIEngine::removeUIComponent(UIComponent* uic) {
  _uiComponents.remove(uic);
}

void UIEngine::display() {
  if (glIsList(_dlist)) {
    glCallList(_dlist);
  } else {
    glNewList(_dlist, GL_COMPILE);
    for (std::list<UIComponent*>::iterator it = _uiComponents.begin(); it
        != _uiComponents.end(); it++) {
      (*it)->draw();
    }
    glEndList();
  }
}
